#ifndef _PARTICLE_H_
#define _PARTICLE_H_


#include "SDL.h"
#include <vector>
#include "render_target.h"

//Define a single particle template below to enable the code to make it
#define PARTICLE_1
//#define PARTICLE_2

#define PARTICLE_MAX_SPEED 1500.0

typedef struct PD_Particle {
	double x, y;
	double velx, vely;
	double accx, accy;
	SDL_Surface * image;
	SDL_Rect imageOffset;
	double life, lifeSpan;
	bool alive;
	Uint8 frame;
} PD_Particle;

void SetupParticle(PD_Particle* particle, double x, double y, double velx, double vely, double accx, double accy, SDL_Surface* image);

class ParticleSystem : public RenderTarget
{
protected:
	std::vector<SDL_Surface*> images;
	std::vector<PD_Particle> particles;
	double myLife, myLifeSpan;
	bool coolDown;

public:
	double xrange, yrange; //Start position params
	double velx, vely, velxrange, velyrange; //Start velocity params
	double accx, accy, accxrange, accyrange; //Acceleration params
	bool gravityConstant;
	PD_vect2 gravityDir;
	PD_vect2 gravityWell;
	double gravityWellConstant;
	double life, liferange;
	int maxParticles;
	bool relativeParticles;
	double rate, raterange;
	bool active;
	double systemLife;
	double systemLifeRange;
	bool removeAfterLifeSpan;
    long renderCount;

	ParticleSystem(std::string id);
	~ParticleSystem();

	double getSystemLife();
	void addImage(std::string imageName, SDL_Surface* image);
	void initParticle(PD_Particle* particle);
	void Init();
	void StartSystem();
	void StopSystem();
	void ToggleSystem();
	void ClearSystem();
	void Update(double timeDelta);
	void Render(SDL_Surface* screen);
    void renderParticles(SDL_Surface* screen);
	void calculateScreenCoords(PD_Particle* particle,  Sint16 &rx, Sint16 &ry, bool useImageSize);

	//Templates
	void SetupFireTemplate();
	void SetupSwarmTemplate();
};


class TraceParticleSystem : public ParticleSystem
{
private:
	std::vector<PD_Particle> particleTraces;
public:
	bool renderImages;
	bool renderImagesBelow;

	TraceParticleSystem(std::string id);

	void Init();
	void Update(double timeDelta);
	void Render(SDL_Surface* screen);
};

#endif